//
//  RootViewController.swift
//  Weibo
//
//  Created by 唐润欣 on 16/6/17.
//  Copyright © 2016年 唐润欣. All rights reserved.
//

import UIKit
import HMRefresh

class RootViewController: UIViewController {
    
    /// 导航栏注册按钮
    var refistBarButtonItem: UIBarButtonItem?
    
    /// 导航栏登录按钮
    var loginBarButtonItem: UIBarButtonItem?
    
    //是否登录
    var isLogin = false
    
    //访客视图
    var visitorView: VisitorView?
    
    //tableView
    lazy var tableView: UITableView = UITableView()
    
    //下拉刷新控件
    lazy var refreshControl: HMRefreshControl = HMRefreshControl()
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        setupView()
    }
    
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        
        tableView.frame = view.bounds
    }
}

// MARK: - 设置UI
extension RootViewController {
    
    func setupView() {
        
        view.backgroundColor = UIColor.grayColor()
        
        setupTableView()
        setupRefresh()
        
        if !UserAccount.sharedUserAccount.isLogin {
            
            setupVisitorView()
        }
    }
    
    func setupTableView() {
        
        view.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
    }
    
    func setupRefresh() {
        
        //下拉刷新
        refreshControl.pulldownView = HMPulldownView(frame: CGRectMake(0, 0, screenWidth, 44))
        tableView.addSubview(refreshControl)
        
        refreshControl.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
    }
    
    //实现访客视图
    func setupVisitorView() {
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action: "didLogin")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .Plain, target: self, action: "didLogin")
        
        visitorView = VisitorView()
        visitorView?.delegate = self
        view.addSubview(visitorView!)
    }
}

// MARK: - 获取数据
extension RootViewController {
    
    func loadData() {
        
    }
}

// MARK: - tableView的数据源方法
extension RootViewController: UITableViewDataSource {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 0
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        return UITableViewCell()
    }
}

// MARK: - tableView的代理方法
extension RootViewController: UITableViewDelegate {
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
    }
}

extension RootViewController: VisitorDelegate {
    
    func didLogin() {
        
        let authViewController = AuthViewController()
        let navigationController = UINavigationController(rootViewController: authViewController)
        presentViewController(navigationController, animated: true, completion: nil)
    }
}

